
%Creates struct regrVar with returns and yield spreads for each horizon


%% Load bond data

%Bond yields
load('GSW_yieldsMd_Mat1Md_2016.mat')

dates = yieldsMd_Mat1Md(2:end,1);
yields = log(1+yieldsMd_Mat1Md(2:end,2:end)/100);
mats =  yieldsMd_Mat1Md(1,2:end)';
     
annMats = mats/12;

%CRSP risk-free rates
load('crspRet.mat');

crspRet1 = log(1+crspRet(2:end,2));
crspRet3 = log(1+crspRet(2:end,3));

T = length(yields);

%% Prepare regression variables

%Riskfree rates
rf1 = crspRet1(1+1:end);

rf3 = filter(ones(3,1),1,crspRet3);
rf3 = rf3(1+3:end);

rf6 = yields(1:end-6,mats==6)*6/12;

rf9 = yields(1:end-9,mats==9)*9/12;

rf12 = yields(1:end-12,mats==12);

rf18 = yields(1:end-18,mats==18)*18/12;

%Yield spreads y,t,k-y,t,12
%Excess holding period return xhr,t,k = k*y,t-h,k - (k-h)*y,t,k-h - rf,t,h

veck = (24:3:120)';

numk = length(veck);

%1 month horizon
idxk = ismember(mats,veck);
idxk_1 = ismember(mats,veck-1);
idx12 = ismember(mats,12);

matsk = mats(idxk);
annMatsk = annMats(idxk);

regrVar.h1.spreads = yields(1:end-1,idxk)-repmat(yields(1:end-1,idx12),1,numk); 

regrVar.h1.forwardSpreads = repmat(annMats(idxk)',T-1,1).*yields(1:end-1,idxk) ...
                            -repmat(annMats(idxk_1)',T-1,1).*yields(1:end-1,idxk_1) ...
                            -1/12*repmat(yields(1:end-1,idx12),1,numk);                              
                              
regrVar.h1.returns =  repmat(annMats(idxk)',T-1,1).*yields(1:end-1,idxk) ... 
                     -repmat(annMats(idxk_1)',T-1,1).*yields(1+1:end,idxk_1) ...
                     -repmat(rf1,1,numk);    
 
regrVar.h1.rf = rf1;               

%3 month horizon
idxk_3 = ismember(mats,veck-3);

regrVar.h3.spreads = yields(1:end-3,idxk)-repmat(yields(1:end-3,idx12),1,numk); 

regrVar.h3.forwardSpreads = repmat(annMats(idxk)',T-3,1).*yields(1:end-3,idxk) ...
                            -repmat(annMats(idxk_3)',T-3,1).*yields(1:end-3,idxk_3) ...
                            -3/12*repmat(yields(1:end-3,idx12),1,numk); 
                               

regrVar.h3.returns =  repmat(annMats(idxk)',T-3,1).*yields(1:end-3,idxk) ... 
                     -repmat(annMats(idxk_3)',T-3,1).*yields(1+3:end,idxk_3) ...
                     -repmat(rf3,1,numk); 

regrVar.h3.rf = rf3;               
           
               
%6 month horizon
idxk_6 = ismember(mats,veck-6);

regrVar.h6.spreads = yields(1:end-6,idxk)-repmat(yields(1:end-6,idx12),1,numk); 

regrVar.h6.forwardSpreads = repmat(annMats(idxk)',T-6,1).*yields(1:end-6,idxk) ...
                            -repmat(annMats(idxk_6)',T-6,1).*yields(1:end-6,idxk_6) ...
                            -6/12*repmat(yields(1:end-6,idx12),1,numk); 

regrVar.h6.returns =  repmat(annMats(idxk)',T-6,1).*yields(1:end-6,idxk) ... 
                     -repmat(annMats(idxk_6)',T-6,1).*yields(1+6:end,idxk_6) ...
                     -repmat(rf6,1,numk);
               
regrVar.h6.rf = rf6;                            
               
%9 month horizon
idxk_9 = ismember(mats,veck-9);

regrVar.h9.spreads = yields(1:end-9,idxk)-repmat(yields(1:end-9,idx12),1,numk); 

regrVar.h9.forwardSpreads = repmat(annMats(idxk)',T-9,1).*yields(1:end-9,idxk) ...
                            -repmat(annMats(idxk_9)',T-9,1).*yields(1:end-9,idxk_9) ...
                            -9/12*repmat(yields(1:end-9,idx12),1,numk); 

regrVar.h9.returns =  repmat(annMats(idxk)',T-9,1).*yields(1:end-9,idxk) ... 
                     -repmat(annMats(idxk_9)',T-9,1).*yields(1+9:end,idxk_9) ...
                     -repmat(rf9,1,numk);    
 
regrVar.h9.rf = rf9;                             
               
%12 month horizon
idxk_12 = ismember(mats,veck-12);

regrVar.h12.spreads = yields(1:end-12,idxk)-repmat(yields(1:end-12,idx12),1,numk); 

regrVar.h12.forwardSpreads = repmat(annMats(idxk)',T-12,1).*yields(1:end-12,idxk) ...
                            -repmat(annMats(idxk_12)',T-12,1).*yields(1:end-12,idxk_12) ...
                            -repmat(yields(1:end-12,idx12),1,numk); 

regrVar.h12.returns =  repmat(annMats(idxk)',T-12,1).*yields(1:end-12,idxk) ... 
                      -repmat(annMats(idxk_12)',T-12,1).*yields(1+12:end,idxk_12) ...
                      -repmat(rf12,1,numk);    
  
regrVar.h12.rf = rf12;                               
                
%12 month horizon
idxk_18 = ismember(mats,veck-18);

regrVar.h18.spreads = yields(1:end-18,idxk)-repmat(yields(1:end-18,idx12),1,numk); 

regrVar.h18.forwardSpreads = repmat(annMats(idxk)',T-18,1).*yields(1:end-18,idxk) ...
                            -repmat(annMats(idxk_18)',T-18,1).*yields(1:end-18,idxk_18) ...
                            -repmat(yields(1:end-18,idx12),1,numk); 

regrVar.h18.returns =  repmat(annMats(idxk)',T-18,1).*yields(1:end-18,idxk) ... 
                        -repmat(annMats(idxk_12)',T-18,1).*yields(1+18:end,idxk_18) ...
                        -repmat(rf18,1,numk);    

regrVar.h18.rf = rf18;   

% Clear some variables that are now in the struc
clear rf1 rf3 rf6 rf9 rf12 rf18                
                       





